package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbContentMapper;
import com.dongdongshop.model.TbContent;
import com.dongdongshop.service.ContentService;
import com.dongdongshop.vo.ContentVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ContentServiceImpl implements ContentService {

    @Autowired
    private TbContentMapper tm;

    @Override
    public List<ContentVO> selectAll() {
        List<TbContent> list = tm.selectByExample(null);
        return list.stream().map(tbContent -> {
            ContentVO vo = new ContentVO();
            BeanUtils.copyProperties(tbContent,vo);
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public void addContent(ContentVO vo) {
        TbContent tb = new TbContent();
        BeanUtils.copyProperties(vo,tb);
        tm.insertSelective(tb);
    }

    @Override
    public ContentVO toUpdate(Long id) {
        TbContent tbContent = tm.selectByPrimaryKey(id);
        ContentVO vo = new ContentVO();
        BeanUtils.copyProperties(tbContent,vo);
        return vo;
    }

    @Override
    public void update(ContentVO vo) {
        TbContent tb = new TbContent();
        BeanUtils.copyProperties(vo,tb);
        tm.updateByPrimaryKeySelective(tb);
    }

    @Override
    public void deleteContent(Long[] ids) {
        for (Long id : ids) {
            tm.deleteByPrimaryKey(id);
        }
    }
}
